<?php
class Form_Signup extends Zend_Form
{
    public function init()
    {
        $this->setName('registration');

        $this->addElement('text', 'email', array(
            'label'      => 'email',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'EmailAddress',
                new Zend_Validate_Db_NoRecordExists(array(
                    'table' => 'member',
                    'field' => 'email'
                ))
            )
        ));
        
        $this->addElement('password', 'password', array(
            'label'        => 'password',
            'required'     => true,
            'autocomplete' => 'off',
            'filters'      => array('StringTrim'),
            'validators'   => array(
                'NotEmpty',
                array('StringLength', false, array(
                    'min' => Ext_App::getSetting('length.password.min'),
                    'max' => Ext_App::getSetting('length.password.max')
                    )
                )
            )
        ));

        $this->addElement('password', 'confirm_password', array(
            'label'        => 'confirm_password',
            'required'     => true,
            'filters'      => array('StringTrim'),
            'autocomplete' => 'off',
            'validators'   => array(
                array('Identical', false, array('token' => 'password'))
            )
        ));

        $this->addElement('text', 'username', array(
            'label'        => 'username',
            'required'     => true,
            'filters'      => array('StringTrim'),
            'validators'   => array(
                'NotEmpty',
                'Alnum',
                array('StringLength', false, array(
                    'min' => Ext_App::getSetting('length.username.min'),
                    'max' => Ext_App::getSetting('length.username.max')
                    )
                )
            )
        ));

        $this->addElement('select', 'gender', array(
            'label'        => 'gender',
            'multiOptions' => Model_Member::$gender
        ));

        if (APPLICATION_ENV != 'testing') {
            $recaptcha = new Zend_Service_ReCaptcha(
                Ext_App::getSetting('reCaptcha.publicKey'),
                Ext_App::getSetting('reCaptcha.privateKey')
            );
            $this->addElement('captcha', 'captcha', array(
                'captcha'        => 'ReCaptcha',
                'captchaOptions' => array(
                    'theme'   => 'white',
                    'lang'    => 'ru',
                    'captcha' => 'ReCaptcha',
                    'service' => $recaptcha
                ),
                'ignore'         => true
            ));
        }

        $this->addElement('submit', 'register');
    }

    public function save()
    {
        $data = array_merge(array(
            'role'       => Model_Member::ROLE_MEMBER,
            'status'     => Model_Member::STATUS_NOT_VERIFIED,
            'verify_key' => Ext_App::getToken(Ext_App::getSetting('token.verifyKey.salt'))
        ), $this->getValues());
        $newMember = Ext_App::getTable('Model_DbTable_Member')->createRow($data);
        $newMember->save();
        return $newMember;
    }
}